package cn.ranko.site.service;

import cn.ranko.bean.easyui.DataGridParam;
import cn.ranko.site.mapper.SysUsersMapper;
import cn.ranko.site.model.SysUsers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Created by ZJ08 on 2016/7/26.
 */
@Service
public class SysUsersService {

    @Autowired
    SysUsersMapper sysUsersMapper;

    /**
     * 根据用户名查找其角色
     * @param username
     * @return
     */
    public Set<String> findRoles(String username) {
        List<String> list = sysUsersMapper.findRoles(username);
        Set<String> stringSet = new HashSet<String>();
        for (int i=0;i<list.size();i++){
            stringSet.add(list.get(i));
        }
        return stringSet;
    }

    /**
     * 根据用户名查找其权限
     * @param username
     * @return
     */
    public Set<String> findPermissions(String username) {
        List<String> list = sysUsersMapper.findPermissions(username);
        Set<String> stringSet = new HashSet<String>();
        for (int i=0;i<list.size();i++){
            stringSet.add(list.get(i));
        }
        return stringSet;
    }

    /**
     * 根据用户名查找用户
     * @param username
     * @return
     */
    public SysUsers findByUsername(String username) {
        return sysUsersMapper.findByUsername(username);
    }

    public int createUser(SysUsers users) {
        return sysUsersMapper.insertSelective(users);
    }

    /**
     * 获取有效用户的数量
     * @return
     */
    public int getTotal() {
        return sysUsersMapper.getTotal();
    }

    public List<SysUsers> getRows(DataGridParam dataGridParam) {
        return sysUsersMapper.getRows(dataGridParam);
    }

    /**
     *
     * 根据用户Id获取用户
     *
     * @param userId
     * @return
     */
    public SysUsers getByUserId(Long userId) {
        return sysUsersMapper.selectByPrimaryKey(userId);
    }

    public int deleteByIds(String ids) {
        String[] strings = ids.split(",");
        int affectrow = 0;
        for(int i=0;i<strings.length;i++){
            SysUsers sysUsers = new SysUsers();
            sysUsers.setId(Long.parseLong(strings[i]));
            sysUsers.setLocked(true);
            affectrow +=sysUsersMapper.updateByPrimaryKeySelective(sysUsers);
        }
        return affectrow;
    }
}
